<?php

namespace app\models;

use Yii;
use yii\data\ActiveDataProvider;

/**
 * This is the model class for table "gky_we_article".
 *
 * @property string $id
 * @property integer $weid
 * @property string $wename
 * @property string $addtime
 * @property string $content
 * @property string $puber
 * @property string $title
 * @property string $banner
 * @property integer $typeid
 * @property string $arttype
 * @property string $olink
 */
class GkyWeArticle extends \yii\db\ActiveRecord {

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return 'gky_we_article';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['weid', 'typeid'], 'integer'],
            [['addtime'], 'safe'],
            [['content'], 'string'],
            [['wename', 'puber'], 'string', 'max' => 256],
            [['title', 'banner', 'olink'], 'string', 'max' => 512],
            [['arttype'], 'string', 'max' => 128],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'weid' => '微信编号',
            'wename' => '微信名称',
            'addtime' => '添加时间',
            'content' => '内容',
            'puber' => '发布者',
            'title' => '标题',
            'banner' => '横幅',
            'typeid' => '类型编号',
            'arttype' => '类型名称',
            'olink' => 'Olink',
        ];
    }

    public function beforeSave($insert) {
        if (parent::beforeSave($insert)) {

            //添加时间
            $time = date("Y-m-d H:i:s", time());
            $this->addtime = $time;
            //微信订阅号名称
            $this->wename = $this->account->wename;

            return true;
        } else {
            return false;
        }
    }

    public function getAccount() {
        return $this->hasOne(GkyWeAccount::className(), ['id' => 'weid']);
    }

    public function getArcticleType() {
        return $this->hasOne(GkyWeArticleType::className(), ['id' => 'typeid']);
    }

    public static function findArticlesByTypeId($typeid) {

        $query = GkyWeArticle::find()
                ->where(['typeid' => $typeid])
                ->orderBy(['addtime' => SORT_DESC]);

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);
        return $dataProvider;
    }

    //获得文章列表
    public static function getArticleList($articletypeid, $pageNow, $pageSize) {
        $articletype = GkyWeArticleType::find()
                ->where(['id' => $articletypeid])
                ->all();

        $list = GkyWeArticle::find()
                ->where(['typeid' => $articletypeid])
                ->orderBy(['addtime' => SORT_DESC])
                ->offset(($pageNow - 1) * $pageSize)
                ->limit($pageSize)
                ->all();
        $results = [];


        //提取所需要的元素
        $i = 0;
        for ($i = 0; $i < count($list); $i++) {
            $results['detail'][$i]['id'] = $list[$i]->id;
            $results['detail'][$i]['title'] = $list[$i]->title;
            $results['detail'][$i]['puber'] = $list[$i]->puber;
            $results['detail'][$i]['addtime'] = $list[$i]->addtime;
        }
        if ($i != 0) {
            $results['articletype'] = $articletype[0]->typename;
        }
        return $results;
    }

    //获得文章
    public static function getArticle($articleid) {
        $list = GkyWeArticle::find()
                ->where(['id' => $articleid])
                ->all();
        $articletype = GkyWeArticleType::find()
                ->where(['id' => $list[0]->typeid])
                ->all();

        $results = [];
        $results['articletype'] = $articletype[0]->typename;
        $results['title'] = $list[0]->title;
        $results['content'] = $list[0]->content;

        return $results;
    }

}
